package com.budingge.httpuitls;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * https处理
 * @author budingge.com
 *
 */
public class HttpsHandler {
	public static void trustAllHttpsCertificates() throws Exception {
		TrustManager[] tm_array = new TrustManager[1];
		TrustManager tm = new myTrustManager();
		tm_array[0] = tm;
		SSLContext sc = SSLContext.getInstance("SSL");
		sc.init(null, tm_array, null);
		HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
	}

	public static class myTrustManager implements TrustManager,
			X509TrustManager {
		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public boolean isServerTrusted(X509Certificate[] certs) {
			return true;
		}

		public boolean isClientTrusted(X509Certificate[] certs) {
			return true;
		}

		public void checkServerTrusted(X509Certificate[] certs, String authType)
				throws CertificateException {
			return;
		}

		public void checkClientTrusted(X509Certificate[] certs, String authType)
				throws CertificateException {
			return;
		}
	}
}